package com.aptech.fpt.c0810g.newspj.beans;

import com.aptech.fpt.c0810g.newspj.business.DanTriDataProcess;
import com.aptech.fpt.c0810g.newspj.dataaccess.NewsAction;
import com.aptech.fpt.c0810g.newspj.dataaccess.SourceAction;
import com.aptech.fpt.c0810g.newspj.entity.News;
import com.aptech.fpt.c0810g.newspj.entity.Source;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author zang
 */
@ManagedBean
@ViewScoped
public class GetNews {

    private String url;
    private String message;
    private ArrayList<Source> sources;
    private Source selected = new Source();
    private ArrayList<News> newsList = new ArrayList<News>();
    private boolean flag;
    private News[] selectNews;

    public News[] getSelectNews() {
        return selectNews;
    }

    public void setSelectNews(News[] selectNews) {
        this.selectNews = selectNews;
    }

    public boolean isFlag() {
        return flag;
    }

    public ArrayList<News> getNewsList() {

        return newsList;
    }

    public void setNews(ArrayList<News> news) {
        this.newsList = news;
    }

    public Source getSelected() {
        return selected;
    }

    public void setSelected(Source selected) {
        this.selected = selected;
    }

    public ArrayList<Source> getSources() {
        return sources;
    }

    public void setSources(ArrayList<Source> sources) {
        this.sources = sources;
    }

    public String getMessage() {
        try {
            if (this.selected.getName().length() > 0) {
                message = "Lấy tin từ tờ báo " + selected.getName();
            } else {
                message = "Bạn hãy chọn tờ báo muốn lấy tin về!";
            }
        } catch (Exception e) {
            message = "Bạn hãy chọn tờ báo muốn lấy tin về!";
        }
        return message;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    /** Creates a new instance of GetNews */
    public GetNews() {
        iniGetNews();

    }

    private void iniGetNews() {
        SourceAction sourceAction = new SourceAction();
        this.sources = sourceAction.getSourceList();
    }

    public void doGetNews() {

        try {
            if (this.selected.getName().length() > 0) {
                NewsAction aciton = new NewsAction();
                this.newsList = aciton.doGetNews();
                if (this.newsList.size() > 0) {
                    this.flag = true;
                } else {
                    this.flag = false;
                }
            } else {
                message = "Bạn hãy chọn tờ báo muốn lấy tin về!";
            }
        } catch (Exception e) {
            message = "Bạn hãy chọn tờ báo muốn lấy tin về!";
        }
    }

    public void doProcess() {
        //     this.url = "" + selectNews.length;
        if (this.selectNews.length > 0) {
            ArrayList<News> mainContentNews = this.getMainContent();
            NewsAction newsAction = new NewsAction();
            try {
                boolean retuls = newsAction.insertNewsContent(mainContentNews);
                if (retuls) {
                    this.url = "Chú ý : Đã lấy tin thành công!";
                } else {
                    this.url = "Chú ý : Lấy tin không thành công!";
                }
            } catch (SQLException ex) {
              
            }
        } else {
            this.url = "Chú ý : Bạn chưa chọn item nào!";
        }
    }

    /*
     * Tra ve noi dung chinh cua tin tuc
     */
    private ArrayList<News> getMainContent() {
        ArrayList<News> array = new ArrayList<News>();
        //bat dau lay noi dung chinh cua cac trang
        for (int i = 0; i < this.selectNews.length; i++) {
            News newsItem = this.selectNews[i];
            try {
                String mainContent = new DanTriDataProcess().getNodeList(newsItem.getSourceLink(), "fon34 mt3 mr2 fon43").item(0).getTextContent();
                newsItem.setMainContent(mainContent);
                newsItem.setSourceID(this.selected.getId());
                newsItem.setCatelogy(1);
                newsItem.setMessageResult("Thành công !");
            } catch (Exception e) {
                newsItem.setMessageResult("Không thành công !");
            } finally {
                array.add(newsItem);
            }
        }
        return array;
    }
}
